package com.gardeneaters.game_v0.autoplay;

class Medoid {

	int [] arrayX;
	int [] arrayY;
	int totalNumOfElem;
	
	protected Medoid(int x, int y){
		arrayX = new int[x];
		arrayY = new int[y];
		totalNumOfElem=0;
		for(int i=0; i<x; i++)
		{
			arrayX[i] = 0;
		}
		for(int i=0; i<y; i++)
		{
			arrayY[i] = 0;
		}
		
	}
	
	protected void addHex(int i, int j){
		//if(i>=NumOfHexY || j>=NumOfHexX)
			//System.out.println("ATTENSION");
		arrayX[j]++;
		arrayY[i]++;
		totalNumOfElem++;
	}
	
	protected int[] getMedoid(){
		int [] ij = new int[2];
		int medoid;
		
		if(totalNumOfElem%2!=0)//Odd number
		{
			medoid = (int) Math.ceil(totalNumOfElem/2.0);// in case it is 0.5, 1.5, 2.5 ...
		}
		else//following is not correct but there is no need for it to b otherwise. we dnt need 2 elem avrg as in median
		{
			medoid = totalNumOfElem/2;
		}
		//System.out.println("medoid is "+medoid+" NumOfElem "+totalNumOfElem);
		int temp=0;
		for(int k=0; k<arrayX.length; k++)
		{
			for(int l=0; l<arrayX[k]; l++)
			{
				temp++;
				if(temp==medoid)
					ij[1] = k;
			}
		}
		temp=0;
		for(int k=0; k<arrayY.length; k++)
		{
			for(int l=0; l<arrayY[k]; l++)
			{
				temp++;
				if(temp==medoid)
					ij[0] = k;
			}
		}
		
		return ij;
	}
	
}
